import config from "./config.js"

const request = {};

request.get = (url, params) => {
	let headers = {};
	const token = uni.getStorageSync('token');
	if (token) {
		headers['x-token'] = token;
	}
	uni.showLoading({
		title: '加载中...'
	});
	return new Promise((resolve, reject) => {
		uni.request({
			url: config.baseUrl + url,
			method: 'GET',
			data: params,
			dataType: 'json',
			header: headers,
			success: (res) => {
				uni.hideLoading();
				if (res.data.code === 0) {
					return resolve(res.data);
				} else {
					uni.showToast({
						title: res.data.msg,
						icon: 'none'
					})
				}
			},
			fail: (error) => {
				uni.hideLoading();
				uni.showToast({
					title: '网络不给力,请稍后重试',
					icon: 'none'
				})
			}
		})
	});
}

request.getNoLoading = (url, params) => {
	let headers = {};
	const token = uni.getStorageSync('token');
	if (token) {
		headers['x-token'] = token;
	}
	return new Promise((resolve, reject) => {
		uni.request({
			url: config.baseUrl + url,
			method: 'GET',
			data: params,
			dataType: 'json',
			header: headers,
			success: (res) => {
				return resolve(res.data);
			},
			fail: (error) => {
				uni.hideLoading();
				uni.showToast({
					title: '网络不给力,请稍后重试',
					icon: 'none'
				})
			}
		})
	});
}

request.post = (url, params) => {
	let headers = {};
	const token = uni.getStorageSync('token');
	if (token) {
		headers['x-token'] = token;
	}
	console.log(headers);
	uni.showLoading({
		title: '加载中...'
	});
	return new Promise((resolve, reject) => {
		uni.request({
			url: config.baseUrl + url,
			method: 'POST',
			data: params,
			header: headers,
			dataType: 'json',
			success: (res) => {
				uni.hideLoading();
				if (res.data.code === 0) {
					return resolve(res.data);
				} else {
					uni.showToast({
						title: res.data.msg,
						icon: 'none'
					})
				}
			},
			fail: (error) => {
				uni.hideLoading();
				uni.showToast({
					title: '网络不给力,请稍后重试',
					icon: 'none'
				})
			}
		})
	});
}

request.postNoLoading = (url, params) => {
	let headers = {};
	const token = uni.getStorageSync('token');
	if (token) {
		headers['x-token'] = token;
	}
	return new Promise((resolve, reject) => {
		uni.request({
			url: config.baseUrl + url,
			method: 'POST',
			data: params,
			dataType: 'json',
			header: headers,
			success: (res) => {
				if (res.data.isSucc) {
					return resolve(res.data);
				} else {
					uni.showToast({
						title: res.data.err.message,
						icon: 'error'
					})
				}
			},
			fail: (error) => {
				uni.showToast({
					title: '网络不给力，请稍后重试',
					icon: 'none'
				})
			}
		})
	});
}

request.uploadFile = (url, filePath) => {
	let headers = {};
	const token = uni.getStorageSync('token');
	if (token) {
		headers['x-token'] = token;
	}
	uni.showLoading({
		title: '上传中...'
	});
	return new Promise((resolve, reject) => {
		uni.uploadFile({
			url: config.baseUrl + url,
			filePath: filePath,
			header: headers,
			dataType: 'json',
			name: 'files',
			success: (res) => {
				res.data = JSON.parse(res.data);
				uni.hideLoading();
				if (res.data.code === 0) {
					return resolve(res.data);
				} else {
					uni.showToast({
						title: res.data.msg,
						icon: 'none'
					})
				}
			},
			fail: (error) => {
				uni.hideLoading();
				uni.showToast({
					title: '网络不给力,请稍后重试',
					icon: 'none'
				})
			}
		})
	});
}

export default request;